<?php

abstract class DatabaseQuery {

	protected static $operations = array('SELECT'=>array('fetchResult'=>true), 'DELETE'=>array('fetchResult'=>false), 'UPDATE'=>array('fetchResult'=>false));

	protected $condition = array(); //$data
	protected $queryFields = array();
	protected $selectFields = array();
	protected $limit = 0;
	protected $sortBy = null;
	protected $groupBy = null;
	protected $having = null;
	protected $count = null;
	protected $asCount = false;
	protected $cache = true;
	protected $join = null;
	protected $cacheTime = 0;
	protected $cacheObject = 0;
	protected $obj;
	protected $updateFileldArray;
	protected $id;
	protected $fieldName;
	protected $databaseType = 'db';
	protected $dataArray = array();
	protected $operationType = 'SELECT';
	protected $onDuplicateKey;
	protected $createSql;
	protected $tableData;


	public function getCreateSql() {
	 return $this->createSql;
	}

	public function setCreateSql($createSql) {
	 $this->createSql = $createSql;
	}

	
	public function getOperationSettings($setting) {
		return self::$operations[$this->getOperationType()][$setting];
	}

	public function getOperationType() {
		return $this->operationType;
	}

	public function setOperationType($operationType) {
		if(isset(self::$operations[$operationType])) {
			$this->operationType = $operationType;
		} else {
			throw new CoreException('Niezidentyfikowany typ operacji SQL!');
		}
	}

	public function getCacheTime() {
		return $this->cacheTime;
	}

	public function setCacheTime($cacheTime) {
		$this->cacheTime = $cacheTime;
	}

	public function getOnDuplicateKey() {
		return $this->onDuplicateKey;
	}

	public function setOnDuplicateKey($onDuplicateKey) {
		$this->onDuplicateKey = $onDuplicateKey;
	}
	
	public function getObjClassName() {
		return $this->objClassName;
	}

	public function setObjClassName($objClassName) {
		$this->objClassName = $objClassName;
	}

	public function getObjClassTable() {
		return $this->objClassTable;
	}

	public function setObjClassTable($objClassTable) {
		$this->objClassTable = $objClassTable;
	}

	public function getObjClassTablePK() {
		return $this->objClassTablePK;
	}

	public function setObjClassTablePK($objClassTablePK) {
		$this->objClassTablePK = $objClassTablePK;
	}

	public function getOptClass() {
		return $this->optClass;
	}

	public function setOptClass($optClass) {
		$this->optClass = $optClass;
	}

	public function getCondition() {
		return $this->condition;
	}

	public function setCondition($condition) {
		$this->condition = $condition;
	}

	public function addCondition($field, $value, $condition = null, $split = null) {
		if ($condition === null) {
			$this->condition[$field][] = $value;
		} else {
			$this->condition[$field][] = array('value' => $value, 'split' => $split, 'condition' => $condition);
		}
	}

	public function getQueryFields() {
		return $this->queryFields;
	}

	public function setQueryFields($queryFields) {
		$this->queryFields = $queryFields;
	}
	
	public function setSelectFields($selectFields) {
		$this->selectFields = $selectFields;
	}

	public function getLimit() {
		return $this->limit;
	}

	public function setLimit($limit) {
		$this->limit = $limit;
	}

	public function getSortBy() {
		return $this->sortBy;
	}

	public function setSortBy($sortBy) {
		$this->sortBy = $sortBy;
	}

	public function getCount() {
		return $this->count;
	}

	public function setCount($count) {
		$this->count = $count;
	}
	
	public function setAsCount($asCount)
	{
		$this->asCount = $asCount;
	}
	
	public function getAsCount()
	{
		return $this->asCount;
	}

	public function getCache() {
		return $this->cache;
	}

	public function setCache($cache) {
		$this->cache = $cache;
	}

	public function getJoin() {
		return $this->join;
	}

	public function setJoin($join) {
		$this->join = $join;
		return $this;
	}

	public function addJoin($name,$value,$type = 'LEFT') {
		$this->join[$type][$name] = $value;
	}

	public function getCacheObject() {
		return $this->cacheObject;
	}

	public function setCacheObject($cacheObject) {
		$this->cacheObject = $cacheObject;
	}

	public function getObj() {
		return $this->obj;
	}

	public function setObj($obj) {
		$this->obj = $obj;
	}

	public function getUpdateFileldArray() {
		return $this->updateFileldArray;
	}

	public function setUpdateFileldArray($updateFileldArray) {
		$this->updateFileldArray = $updateFileldArray;
	}

	public function getId() {
		return $this->id;
	}

	public function setId($id) {
		$this->id = $id;
	}

	public function getFieldName() {
		return $this->fieldName;
	}

	public function setFieldName($fieldName) {
		$this->fieldName = $fieldName;
	}

	public function getDatabaseType() {
		return $this->databaseType;
	}

	public function setDatabaseType($databaseType) {
		$this->databaseType = $databaseType;
	}

	public function getDataArray($key) {
		if(isset($this->dataArray[$key]))
			return $this->dataArray[$key];
		else
			return null;
	}

	public function setDataArray($key,$value) {
		$this->dataArray[$key] = $value;
	}

	public function getSelectFields() {
		return $this->selectFields;
	}

	public function getSelectFieldsQuery() {
		if(count($this->selectFields) > 0 ) {
			$q = " , ";
			foreach($this->selectFields as $key => $value) {
				$q.= $value . " as " . $key . ",";
			}
			return substr($q, 0, strlen($q)-1);
		}
		else
			return null;
	}

	public function addSelectFields($key,$selectFields) {
		if($key == null)
			$this->selectFields[] = $selectFields;
		else
			$this->selectFields[$key] = $selectFields;
	}

	public function getGroupBy() {
		return $this->groupBy;
	}

	public function setGroupBy($groupBy) {
		$this->groupBy = $groupBy;
	}
	public function setOrderBy($order) {
		return $this->setSortBy($order);
	}
	
	public function getHaving() {
		return $this->having;
	}

	public function setHaving($having) {
		$this->having = $having;
	}

	public function setTableData($table_data) {
		$this->tableData = $table_data;
		$this->setLimit(($table_data['p'] - 1) * $table_data['t'].', '.$table_data['t']);
		$this->setSortBy($table_data['o'].' '.$table_data['v']);
	}

	public function getTableData() {
		return $this->tableData;
	}
	
	
	public static function toSelect($className,$tableName,$queryFields = null, $alias = null)
	{
	    //$return = '*';

	    $return = null;
	    $stmt = '';
	    $list = '';
	    $prfx = '';
	    if(!empty($alias) && is_string($alias)) {
		$tableName = $alias;
		$prfx = $alias . '_';
	    } 

	    $class = new $className();
	    $fields = $class->getFields();
	    
	    if(count($queryFields) > 0)
	    {
		    foreach ($queryFields as $key => $var)
		    {
			    if(isset($fields[$var]) && $fields[$var]!="")
				    $list .= $tableName.'.'.$var.' AS '. $prfx.$className.'_'.$var.', ';
		    }
	    }
	    else
	    {
		    foreach ($fields as $key => $var)
		    {
			    if($key[0] == strtolower($key[0]))
				    $list .= $tableName.'.'.$key.' AS '. $prfx.$className.'_'.$key.', ';
		    }
	    }

	    $list = substr($list, 0, strlen($list) - 2);
	    $return = $list;
		    
	    return $return;
	}

}

